// tarselect.js
var app = getApp()
var host = app.globalData.host
Page({
  data: {
    image: app.globalData.image,
    pageNo:1,
    selectid:'',
    idarr:[],
    checkwhich:true,
    open:false,
    idname:'',
    namearr:[],
    origincontent:[],
    sureorigincontent:[],
    checknumber : 0,
    showlist:false
  },
  onLoad: function (options) {
    var that = this
    var origincontent = []
    wx.request({
      url: host + '/OperationApi/selSelectedContent?pageNo=' + that.data.pageNo,
      header: { 'content-type': 'application/json' },
      success:function(res){
        console.log(res)
        var listTag = res.data.data.listTag
        listTag.forEach(item => {
          item.status = 0
        })
        that.setData({
          origincontent: res.data.data.listSelectedContent,
          content: res.data.data.listSelectedContent,
          listall: listTag,
          alllist: listTag.length,
          listallsame: listTag,
          pageNo: that.data.pageNo + 1
        })
        console.log(that.data.listall)       
      }
    })
  },
  // 展开查看
  open:function(){
      this.setData({
        open: true,
      })
  },
  // 查看全部精选内容
  seeall:function(event){
    var that = this
    var listTag = this.data.listall
    for (var i in listTag){
      listTag[i].status = 0
    }  
    this.setData({
        selectid : '',
        checkwhich:true,
        listall: listTag,
        checknumber:0
    })
  },
  // 查看选择的
  toselect:function(event){
    var that = this;
    var secid = event.currentTarget.dataset.secid;
    var index = event.currentTarget.dataset.index;
    var listTag = that.data.listall;
    if (listTag[index].status == 0){
      listTag[index].status = 1;
      that.data.checknumber ++;
    }else{
      listTag[index].status = 0;
      that.data.checknumber --;
    }
    if (that.data.checknumber == 0){
      that.setData({  
        checkwhich: true,
        listall: listTag,
        checkall: that.data.checknumber,
        selectid: ''
      })
    }else{
      that.setData({
        checkwhich: false,
        listall: listTag,
        checkall: that.data.checknumber,
        selectid: secid,
      })
    }
  },
  // 确定选择
  sure:function(){
    var that = this
    var idarr = []
    var namearr = []
    for (var j = 0; j < that.data.listall.length;j++){
      var nameobject = {}
      if (that.data.listall[j].status == 1){
        idarr.push(that.data.listall[j].id)
        nameobject.name = that.data.listall[j].name
        namearr.push(nameobject)
      }
    }
    if (idarr.length == 0) {
      that.setData({
        origincontent: [],
        sureorigincontent: [],
        content: [],
        open: false,
        listallsame: that.data.listall,
        pageNo: 1,
        namearr: namearr,
        idstr: idarr.join(),
        showlist:true
      })
    }else{
      that.setData({
        origincontent: [],
        sureorigincontent: [],
        content: [],
        open: false,
        listallsame: namearr,
        pageNo: 1,
        namearr: namearr,
        idstr: idarr.join(),
        showlist:false
      })
    }
    that.load()
  },
  load:function(){
    var that = this
    if (that.data.namearr.length == 0) {
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: host + '/OperationApi/selSelectedContent?pageNo=' + that.data.pageNo,
        header: { 'content-type': 'application/json' },
        success: function (res) {
          console.log(res)
          var reslength = res.data.data.listSelectedContent
          if (reslength.length == 4) {        
            that.setData({
              content: that.data.origincontent.concat(reslength),
              origincontent: that.data.origincontent.concat(reslength),
              pageNo: that.data.pageNo + 1
            })
          } else if (0 < reslength.length && reslength.length < 4) {
            that.setData({
              content: that.data.origincontent.concat(reslength),
              pageNo: that.data.pageNo + 1
            })
          } else {
            wx.showToast({
              title: '没有更多了',
              icon: 'success',
              duration: 2000
            })
          }
        }
      })
    } else {
      wx.request({
        url: host + '/OperationApi/selTagContent?tagId=' + that.data.idstr + '&pageNo=' + that.data.pageNo,
        header: { 'content-type': 'application/json' },
        success: function (res) {
          console.log(res)
          var reslength = res.data.data
          if (reslength.length == 4) {
            wx.showLoading({
              title: '加载中',
            })
            that.setData({
              content: that.data.sureorigincontent.concat(reslength),
              sureorigincontent: that.data.sureorigincontent.concat(reslength),
              pageNo: that.data.pageNo + 1
            })
          } else if (0 < reslength.length && reslength.length < 4) {
            wx.showLoading({
              title: '加载中',
            })
            that.setData({
              content: that.data.sureorigincontent.concat(reslength),
              pageNo: that.data.pageNo + 1
            })
          } else {
            wx.showToast({
              title: '没有更多了',
              icon: 'success',
              duration: 2000
            })
          }
        }
      })
    }
    setTimeout(function () {
      wx.hideLoading()
    }, 500)
  },
  onReachBottom: function () {
    var that = this;
    that.load()   
  },
  // 跳转至专题
  toselectpage: function (event) {
    var selectid = event.currentTarget.dataset.selectid
    wx.navigateTo({
      url: '../select/select?id=' + selectid,
    })
  },
  // 跳转至商品详情页
  towed: function (event) {
    var wedid = event.currentTarget.dataset.wedid
    wx.navigateTo({
      url: '../wedproductdetail/wedproductdetail?id=' + wedid,
    })
  }
})